package org.odk.collect.android.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.odk.collect.android.R;
import org.odk.collect.android.model.Project;

import java.util.List;

public class FormListAdapter extends ArrayAdapter<Project> {

    private LayoutInflater mInflater;

    public FormListAdapter(Context context, final List<Project> projecs) {
        super(context, R.layout.lst_item_form, projecs);
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;

        final Project p = getItem(position); // fetches the item from the passed array (in the constructor)

        // reusing the convert view (previous used child view group), to avoid
        // unnecessary code logic execution that's the same for all items anyway.
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.lst_item_form, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.id = p.getId();
        holder.txtDescription.setText(p.getExternalUid());
        holder.txtTitle.setText(p.getTitle());
//        holder.imgView.setImageResource(p.getImageId());
        
        // divider
        /*if (position==0 || !c.getDegree().equals(getItem(position-1).getDegree())) {
            holder.txtDivider.setText( c.getDegree() );
            holder.txtDivider.setVisibility(View.VISIBLE);
        } else {
            holder.txtDivider.setVisibility(View.GONE);
        }*/
        

        return convertView;
    }


    // this is just a helper class to hold all the views of a child view group.
    class ViewHolder {
        Integer id;
        ImageView imgView;
        TextView txtTitle;
        TextView txtDivider;
        TextView txtDescription;

        public ViewHolder(final View view) {
            txtDivider = (TextView) view.findViewById(R.id.txt_divider);
            txtTitle = (TextView) view.findViewById(R.id.txt_title);
            txtDescription = (TextView) view.findViewById(R.id.txt_description);
            imgView = (ImageView) view.findViewById(R.id.img_thumbnail);
        }
    }
}
